package com.pearlsoft.rms.vcenter;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.pearlsoft.rms.VmConstants;
import com.pearlsoft.rms.sample.FakeSSLSocketFactory;
import com.vmware.vcloud.sdk.VCloudException;
import com.vmware.vcloud.sdk.VcloudClient;
import com.vmware.vcloud.sdk.constants.Version;

/**
 * 
 * @author rainshow
 * @Created 2012-5-17 下午05:57:51
 */

public class VcloudConnection {
    private VcloudClient client;
    private static Log logger = LogFactory.getLog(VcloudConnection.class);
    private String url;
    private String userName;
    private String password;

    public VcloudConnection(String url, String userName, String password) {
        this.url = url;
        this.userName = userName;
        this.password = password;
    }

    public VcloudClient getClient() {
        return client;
    }

    public void connect() throws VCloudException {
        logger.info("开始连接服务器url:" + VmConstants.CLOUD_URL);
        VcloudClient vcloudClient = new VcloudClient(url, Version.V1_5);
        try {
            vcloudClient.registerScheme("https", 443, FakeSSLSocketFactory.getInstance());
            vcloudClient.login(userName, password);
        } catch (Exception e) {
            throw new VCloudException(e.getMessage());
        }
        this.client = vcloudClient;
    }

}
